<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <my-bread>个人设置</my-bread>
      </div>
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="编号"> {{ form.id }}</el-form-item>
            <el-form-item label="手机">{{ form.mobile }}</el-form-item>

            <el-form-item label="名称">
              <el-input v-model="form.name" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="简介">
              <el-input
                type="textarea"
                :rows="3"
                v-model="form.intro"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>

            <el-form-item label="邮箱">
              <el-input
                placeholder="请输入内容"
                v-model="form.email"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" plain size="small" @click="save"
                >保存设置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传图片 -->
          <el-upload
            :http-request="uploadAvatar"
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="form.photo" :src="form.photo" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="text">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import eventbus from '@/common/eventbus.js'
export default {
  name: 'setting',
  data () {
    return {
      // imageUrl: null,
      form: {
        name: null,
        email: null,
        intro: null,
        photo: null
      }
    }
  },
  created () {
    this.getUserProfile()
  },
  methods: {
    async uploadAvatar (result) {
      try {
        // console.log(result); result.file
        const fd = new FormData()
        fd.append('photo', result.file)
        const {
          data: { data }
        } = await this.$http.patch('/mp/v1_0/user/photo', fd)
        this.$message.success('修改头像成功')

        this.form.photo = data.photo
        eventbus.$emit('updateUserPhoto', data.photo)
      } catch (e) {
        this.$message.error('修改头像失败')
      }
    },
    async save () {
      const { name, intro, email } = this.form
      try {
        const {
          data: { data }
        } = await this.$http.patch('/mp/v1_0/user/profile', {
          name,
          intro,
          email
        })
        this.$message.success('保存设置成功')
        // this.form = data;
        // 传值
        eventbus.$emit('changeUserName', name)
      } catch (e) {
        this.$message.error('保存设置失败')
      }
    },
    async getUserProfile () {
      const {
        data: { data }
      } = await this.$http.get('/mp/v1_0/user/profile')
      this.form = data
    }
  }
}
</script>

<style scoped lang="less">
.text {
  text-align: center;
}
</style>
